//
//  BaseAPIManager.m
//  SUZHOUBANK
//
//  Created by saberLily on 16/4/11.
//  Copyright © 2016年 RuiFeng-WGCC. All rights reserved.
//

#import "BaseAPIManager.h"

#import "DefineHeader.h"

@implementation BaseAPIManager

+ (void)requestURL:(NSString *)urlString parameters:(NSDictionary *)parameter completion:(HFinished)finished{
    
    NSString *completeURLStr = [NSString stringWithFormat:@"%@/%@",kBaseURL,urlString];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    manager.requestSerializer.timeoutInterval = 10.0f;
    
    [manager POST:completeURLStr parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            NSDictionary *dic = [NSDictionary dictionaryWithObject:responseObject forKey:@"dic"];
            finished(true,200,dic,nil);
        } else if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if (finished) {
                finished(true,200,responseObject,nil);

            }
       
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (finished) {
            finished(false,-1,nil,error);
        }
        
    }];
    
}

+(void)getProductsWithID:(NSString *)proID completion:(HFinished)finished{
    [BaseAPIManager requestURL:@"protocol_company_product!detail.action" parameters:@{@"id":proID} completion:^(bool success, NSInteger status, NSDictionary *data, NSError *error) {
        
        if (success) {
            finished(true,200,data,nil);
        }else{
            finished(false,999,nil,[NSError errorWithDomain:@"DataFormatError" code:999 userInfo:nil]);
        }
        
    }];
}
@end
